// // ******************************************************************
// //       /\ /|       @file       SystemService.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-03-01 11:03
// //    *(__\_\        @Copyright  Copyright (c) , 2025-03-01 11:03 Await
// // ******************************************************************

using AutomaticSubtitles.Service.Infrastructure.Configuration;
using AutomaticSubtitles.Service.Infrastructure.Helper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.ComponentModel;
using System.Threading.Tasks;

namespace AutomaticSubtitles.Service.Services;

/// <summary>
/// 系统服务
/// </summary>
public class SystemService : BaseService<SystemService>
{
    private readonly ILogger<SystemService> _logger;
    private readonly SubtitleExtractionConfig _subtitleConfig;

    public SystemService(ILogger<SystemService> logger, SubtitleExtractionConfig subtitleConfig)
    {
        _logger = logger;
        _subtitleConfig = subtitleConfig;
    }

    /// <summary>
    /// 清理临时文件
    /// </summary>
    /// <param name="hours">保留时间（小时），为0则使用配置值</param>
    /// <param name="cleanFinalDir">是否清理final目录</param>
    /// <returns>清理结果</returns>
    [EndpointSummary("清理临时文件")]
    [Tags("系统管理")]
    [EndpointDescription("清理字幕提取过程中产生的临时文件")]
    [Authorize]
    public ResponseModel<int> CleanupTempFiles(
        [Description("保留时间（小时），为0则使用配置值")] int hours = 0,
        [Description("是否清理final目录")] bool? cleanFinalDir = null)
    {
        // 使用传入的参数或配置值
        var retentionHours = hours > 0 ? hours : _subtitleConfig.TempFileRetentionHours;
        var cleanFinal = cleanFinalDir ?? _subtitleConfig.CleanFinalDirectory;

        _logger.LogInformation($"手动清理临时文件，保留期：{retentionHours}小时，清理final目录：{cleanFinal}");

        // 执行清理
        var deletedCount = AudioSubtitleHelper.CleanupTempFiles(retentionHours, cleanFinal);

        _logger.LogInformation($"临时文件清理完成，共删除 {deletedCount} 个文件");

        return deletedCount.CreateResult();
    }

    /// <summary>
    /// 获取字幕提取配置
    /// </summary>
    /// <returns>字幕提取配置</returns>
    [EndpointSummary("获取字幕提取配置")]
    [Tags("系统管理")]
    [EndpointDescription("获取当前字幕提取相关的配置信息")]
    [Authorize]
    public ResponseModel<SubtitleExtractionConfig> GetSubtitleExtractionConfig()
    {
        return _subtitleConfig.CreateResult();
    }

    /// <summary>
    /// 更新字幕提取配置
    /// </summary>
    /// <param name="config">新的配置</param>
    /// <returns>更新结果</returns>
    [EndpointSummary("更新字幕提取配置")]
    [Tags("系统管理")]
    [EndpointDescription("更新字幕提取相关的配置信息")]
    [Authorize]
    public ResponseModel<bool> UpdateSubtitleExtractionConfig([FromBody] SubtitleExtractionConfig config)
    {
        // 更新配置对象的属性
        _subtitleConfig.TempFileRetentionHours = config.TempFileRetentionHours;
        _subtitleConfig.CleanFinalDirectory = config.CleanFinalDirectory;
        _subtitleConfig.SaveSubtitleToMediaDirectory = config.SaveSubtitleToMediaDirectory;
        _subtitleConfig.SkipExistingLanguages = config.SkipExistingLanguages;
        _subtitleConfig.AutoCleanupIntervalHours = config.AutoCleanupIntervalHours;
        _subtitleConfig.EnableAutoCleanup = config.EnableAutoCleanup;
        _subtitleConfig.LargeLibraryTempFileRetentionHours = config.LargeLibraryTempFileRetentionHours;
        _subtitleConfig.LargeLibraryThreshold = config.LargeLibraryThreshold;
        _logger.LogInformation("字幕提取配置已更新");
        return true.CreateResult();
    }
}